import { createRouter, createWebHistory } from 'vue-router';
import Index from "@/views/User/Index.vue";
import Login from "@/views/Auth/Login.vue";
import Home from "@/views/Home.vue";

// Auth
import Dashboard from "@/views/User/Dashboard/Dashboard.vue";

// Analytics
import WorkloadStatistics from "@/views/User/Analytics/WorkloadStatistics.vue";

// Appointments
import AppointmentList from "@/views/User/Appointments/AppointmentList.vue";
import AppointmentSettings from "@/views/User/Appointments/AppointmentSettings.vue";
import CheckItemManagement from "@/views/User/Appointments/CheckItemManagement.vue";
import PackageManagement from "@/views/User/Appointments/PackageManagement.vue";

// Assessments
import PhysicalFitness from "@/views/User/Assessments/PhysicalFitness.vue";
import Psychological from "@/views/User/Assessments/Psychological.vue";
import Risk from "@/views/User/Assessments/Risk.vue";

// Interventions
import FollowUpDiary from "@/views/User/Interventions/FollowUpDiary.vue";
import IndicatorManagement from "@/views/User/Interventions/IndicatorManagement.vue";
import InterventionPlan from "@/views/User/Interventions/InterventionPlan.vue";
import PopulationClassification from "@/views/User/Interventions/PopulationClassification.vue";
import TemplateManagement from "@/views/User/Interventions/TemplateManagement.vue";

// KnowledgeBase
import AssessmentRecommendations from "@/views/User/KnowledgeBase/AssessmentRecommendations.vue";
import DietaryLibrary from "@/views/User/KnowledgeBase/DietaryLibrary.vue";
import DiseaseLibrary from "@/views/User/KnowledgeBase/DiseaseLibrary.vue";
import ExerciseProgramLibrary from "@/views/User/KnowledgeBase/ExerciseProgramLibrary.vue";
import HealthPlans from "@/views/User/KnowledgeBase/HealthPlans.vue";

// Members
import HealthCheckUpload from "@/views/User/Members/HealthCheckUpload.vue";
import MemberProfile from "@/views/User/Members/MemberProfile.vue";
import MemberStatistics from "@/views/User/Members/MemberStatistics.vue";

// Settings
import MenuSettings from "@/views/User/Settings/MenuSettings.vue";
import PermissionManagement from "@/views/User/Settings/PermissionManagement.vue";
import QuestionBankManagement from "@/views/User/Settings/QuestionBankManagement.vue";
import SurveyManagement from "@/views/User/Settings/SurveyManagement.vue";
import UserManagement from "@/views/User/Settings/UserManagement.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [


    // Auth
    {
      path: '/login',
      name: 'Login',
      component: Login,
    },

    // User
    {
      path: '/',
      redirect: '/dashboard',
      component: Index,
      meta: { requiresAuth: true },
      children: [
        // Home
        {path: 'index', name: 'Home', component: Home},
        // Dashboard
        {path: 'dashboard', name: 'Dashboard', component: Dashboard},

        // Analytics
        {path: 'analytics/workload-statistics', name: 'WorkloadStatistics', component: WorkloadStatistics,},

        // Appointments
        {path: 'appointments/appointment-list', name: 'AppointmentList', component: AppointmentList,},
        {path: 'appointments/appointment-settings', name: 'AppointmentSettings', component: AppointmentSettings,},
        {path: 'appointments/check-item-management', name: 'CheckItemManagement', component: CheckItemManagement,},
        {path: 'appointments/package-management', name: 'PackageManagement', component: PackageManagement,},

        // Assessments
        {path: 'assessments/physical-fitness', name: 'PhysicalFitness', component: PhysicalFitness,},
        {path: 'assessments/psychological', name: 'Psychological', component: Psychological,},
        {path: 'assessments/risk', name: 'Risk', component: Risk,},

        // Interventions
        {path: 'interventions/follow-up-diary', name: 'FollowUpDiary', component: FollowUpDiary,},
        {path: 'interventions/indicator-management', name: 'IndicatorManagement', component: IndicatorManagement,},
        {path: 'interventions/intervention-plan', name: 'InterventionPlan', component: InterventionPlan,},
        {path: 'interventions/population-classification', name: 'PopulationClassification', component: PopulationClassification,},
        {path: 'interventions/template-management', name: 'TemplateManagement', component: TemplateManagement},

        // KnowledgeBase
        {path: 'knowledge-base/assessment-recommendations', name: 'AssessmentRecommendations', component: AssessmentRecommendations,},
        {path: 'knowledge-base/dietary-library', name: 'DietaryLibrary', component: DietaryLibrary,},
        {path: 'knowledge-base/disease-library', name: 'DiseaseLibrary', component: DiseaseLibrary,},
        {path: 'knowledge-base/exercise-program-library', name: 'ExerciseProgramLibrary', component: ExerciseProgramLibrary,},
        {path: 'knowledge-base/health-plans', name: 'HealthPlans', component: HealthPlans,},

        // Members
        {path: 'members/health-check-upload', name: 'HealthCheckUpload', component: HealthCheckUpload,},
        {path: 'members/member-profile', name: 'MemberProfile', component: MemberProfile,},
        {path: 'members/member-statistics', name: 'MemberStatistics', component: MemberStatistics,},

        // Settings
        {path: 'settings/menu-settings', name: 'MenuSettings', component: MenuSettings,},
        {path: 'settings/permission-management', name: 'PermissionManagement', component: PermissionManagement,},
        {path: 'settings/question-bank-management', name: 'QuestionBankManagement', component: QuestionBankManagement,},
        {path: 'settings/survey-management', name: 'SurveyManagement', component: SurveyManagement,},
        {path: 'settings/user-management', name: 'UserManagement', component: UserManagement,},
      ],
    },
  ],
});
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token') // 或根据你的实际存储方式获取登录状态
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)

  if (requiresAuth && !token) {
    // 需要登录但未登录，跳转到登录页并记录目标路径
    next({
      name: 'Login',
      query: { redirect: to.fullPath } // 携带当前路径以便登录后跳回
    })
  } else {
    next()
  }
})

export default router;
